/**
 * @file test_hal_iic.c
 * @author liufeng (liufeng@injoinic.com)
 * @brief 测试硬件iic部分
 * @version 0.1
 * @date 2024-11-14
 *
 * @copyright Copyright (c) 2024  injoinic
 *
 */

#include "bsp_delay.h"
#include "bsp_mpu6050_reg.h"
#include "hal_iic.h"

/**
 * @brief 测试硬件iic，阻塞读写方式
 */
void test_hal_iic()
{
    {
        // 延时所用的定时器初始化
        bsp_Delay_Init();
        // 设置mpu6050采样率和带宽
        uint8_t data = 19;
        hal_i2c_mem_write(&i2c1_handle, 0x68, MPU_SAMPLE_RATE_REG, 0, &data, 1, 10);
        uint8_t lpf = 4;
        hal_i2c_mem_write(&i2c1_handle, 0x68, MPU_CFG_REG, 0, &lpf, 1, 10);
        // mpu6050复位
        data = 0x80;
        hal_i2c_mem_write(&i2c1_handle, 0x68, MPU_PWR_MGMT1_REG, 0, &data, 1, 10);
        Delay_ms(100);
        data = 0x00;
        hal_i2c_mem_write(&i2c1_handle, 0x68, MPU_PWR_MGMT1_REG, 0, &data, 1, 10);
    }

    while (1) {
        // 写一个字节
        {
            uint8_t data = 0x80;
            hal_i2c_mem_write(&i2c1_handle, 0x68, MPU_INTBP_CFG_REG, 0, &data, 1, 10);

            uint8_t ret; // 数据回读
            hal_i2c_mem_read(&i2c1_handle, 0x68, MPU_INTBP_CFG_REG, 0, &ret, 1, 10);
        }
        // 读一个字节
        {
            uint8_t id;
            hal_i2c_mem_read(&i2c1_handle, 0x68, MPU_DEVICE_ID_REG, 0, &id, 1, 10);
        }
        // 读两个字节
        {
            uint8_t data[2];
            hal_i2c_mem_read(&i2c1_handle, 0x68, MPU_TEMP_OUTH_REG, 0, data, 2, 10);
        }
        // 读三个以上字节
        {
            uint8_t data[6];
            hal_i2c_mem_read(&i2c1_handle, 0x68, MPU_ACCEL_XOUTH_REG, 0, data, 6, 10);
        }
        Delay_ms(100);
    }
}

void test_iic_dma_aht21()
{
    // 使用AHT21测试
    // 延时所用的定时器初始化
    bsp_Delay_Init();
    Delay_ms(100);
    while (1) {
        {
            // 测试IIC DMA发送
            uint8_t id = 0;
            hal_i2c_mem_read_dma(&i2c1_handle, 0x38, 0x71, 0, &id, 1);

            uint8_t data[3] = {0xAC, 0x33, 0x00};
            hal_i2c_transmit_dma(&i2c1_handle, 0x38, data, 3);
            Delay_ms(80);
        }

        {
            // 测试IIC DMA接收
            uint8_t res[7] = {0};
            hal_i2c_receive_dma(&i2c1_handle, 0x38, res, 7);
            Delay_ms(100);
        }
    }
}

void test_iic_dma_mpu6050()
{
    // 使用mpu6050测试
    {
        // 延时所用的定时器初始化
        bsp_Delay_Init();
        // 设置mpu6050采样率和带宽
        uint8_t data = 19;
        hal_i2c_mem_write_dma(&i2c1_handle, 0x68, MPU_SAMPLE_RATE_REG, 0, &data, 1);
        uint8_t lpf = 4;
        hal_i2c_mem_write_dma(&i2c1_handle, 0x68, MPU_CFG_REG, 0, &lpf, 1);
        // mpu6050复位
        data = 0x80;
        hal_i2c_mem_write_dma(&i2c1_handle, 0x68, MPU_PWR_MGMT1_REG, 0, &data, 1);
        Delay_ms(100);
        data = 0x00;
        hal_i2c_mem_write_dma(&i2c1_handle, 0x68, MPU_PWR_MGMT1_REG, 0, &data, 1);
    }
    while (1) {
        // 写一个字节
        {
            uint8_t data = 0x80;
            hal_i2c_mem_write_dma(&i2c1_handle, 0x68, MPU_INTBP_CFG_REG, 0, &data, 1);
        }
        // 读一个字节
        {
            uint8_t id;
            hal_i2c_mem_read_dma(&i2c1_handle, 0x68, MPU_DEVICE_ID_REG, 0, &id, 1);
        }
        // 读两个字节
        {
            uint8_t data[2];
            hal_i2c_mem_read_dma(&i2c1_handle, 0x68, MPU_TEMP_OUTH_REG, 0, data, 2);
        }
        // 读三个以上字节
        {
            uint8_t data[6];
            hal_i2c_mem_read_dma(&i2c1_handle, 0x68, MPU_ACCEL_XOUTH_REG, 0, data, 6);
        }
        Delay_ms(100);
    }
}
